//package com.huarui.purchaseOrder.engine;
//
//import org.apache.mina.statemachine.StateMachine;
//import org.apache.mina.statemachine.StateMachineFactory;
//import org.apache.mina.statemachine.StateMachineProxyBuilder;
//import org.apache.mina.statemachine.annotation.Transition;
//import org.apache.mina.statemachine.context.AbstractStateContextLookup;
//import org.apache.mina.statemachine.context.StateContext;
//import org.apache.mina.statemachine.context.StateContextFactory;
//
//import com.huarui.purchaseOrder.model.PurOrSaleOrder;
//import com.huarui.task.service.TaskEngineService;
//
//public class PurchaseEngineProvider {
//	
//	private TaskEngineService  taskEngineService;
//	public TaskEngineService getTaskEngineService() {
//		return taskEngineService;
//	}
//
//	public void setTaskEngineService(TaskEngineService taskEngineService) {
//		this.taskEngineService = taskEngineService;
//	}
//
//	private PurchaseContextDao  purchaseContextDao;
//
//	public PurchaseContextDao getPurchaseContextDao() {
//		return purchaseContextDao;
//	}
//
//	public void setPurchaseContextDao(PurchaseContextDao purchaseContextDao) {
//		this.purchaseContextDao = purchaseContextDao;
//	}
//	
//	public PurchaseEngine  createPurchaseEngine(String startState){
//		StateMachine sm = StateMachineFactory.getInstance(
//				Transition.class).create(
//						startState, new PurchaseEngineHandler());
//
//		PurchaseEngine  engine = new StateMachineProxyBuilder().setStateContextLookup(
//				new AbstractStateContextLookup(new StateContextFactory() {
//
//					@Override
//					public StateContext create() {
//						return new PurchaseContext();
//					}
//				}) {
//
//					@Override
//					protected StateContext lookup(Object eventArg) {
//						
//						int id ;
//						if(eventArg instanceof PurOrSaleOrder){
//							id =   ((PurOrSaleOrder)  eventArg).getId().intValue();
//						}else if(eventArg instanceof  Integer){
//							id =  ((Integer)eventArg).intValue();
//						}else{
//							return null;
//						}
//						return purchaseContextDao.getPurchaseContext(id);
//					}
//
//					@Override
//					protected void store(Object eventArg, StateContext context) {
//						PurchaseContext  purchaseContext= (PurchaseContext) context;
//						purchaseContextDao.insertPurchaseContext(purchaseContext);
//					}
//
//					@Override
//					protected boolean supports(Class<?> c) {
//						
//						return   (c==Integer.class) ||  PurOrSaleOrder.class.isAssignableFrom(c);
//						
//						
//					}
//				}).create(PurchaseEngine.class, sm);
//		
////		engine.setTaskEngineService(taskEngineService);
//		
//		return engine;
//	}
//	
//	public PurchaseEngine  createPurchaseEngine(){
//		
//		return createPurchaseEngine(PurchaseEngineHandler.DRAFTING);
//		
//	}
//	
//	public PurchaseEngine createPurchaseEngine(int id){
//		
//		PurchaseContext context = purchaseContextDao.getPurchaseContext(id) ;
//		
//		if(context==null){
//			return createPurchaseEngine();
//		}
//		
//		return createPurchaseEngine(context.getCurrentState().getId() );
//		
//	}
//
//}
